/*********************/
/** SenseLight.java */
/*********************/
package atag.apps.mainApp;

import atag.runtime.DataItem;
import atag.runtime.DataPool;
import atag.runtime.NodeInfo;
import atag.runtime.actuators.LEDActuator;
import atag.runtime.sensors.LightSensor;

public class SenseLight implements Runnable {

	private DataPool m_dataPool;

	private NodeInfo m_myState;

	/** Creates a new instance of SenseLight */
	public SenseLight(DataPool dp, NodeInfo myconfig) {
		m_dataPool = dp;
		m_myState = myconfig;
	}

	public synchronized void run() {
		
		try {
			while (true) {
				
				Thread.sleep(10000);
				handleExpiryOfTimer();
			}
		} catch (InterruptedException e) {
			return;
		}
	}

	private void handleExpiryOfTimer() {
                LightSensor lightSensor= new LightSensor();
                int lightReading=lightSensor.getCurrentReading();
                LightReading m_Light = new LightReading();
		m_Light.setID(this.m_myState.getMyId());
                m_Light.setLightReading(lightReading);
                LEDActuator lEDActuator= new LEDActuator();
                lEDActuator.enableLEDs();
                lEDActuator.reDrawSomeLEDs(0.5);
                lEDActuator.disableLEDs();
                m_myState.debugPrint("[SensingLight] Got a light reading "+
                        lightReading+" from light sensor");
		m_dataPool.putData(
			new DataItem(IDConstants.D_LIGHTREADING,
			IDConstants.T_SENSELIGHT, m_Light));	}
}
